package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.chat.AllChatBlackModule;

/**
 * 新增接口
 * 举报按钮
 *
 * @ClassName CMD_AllBlackList
 * @Author jingtao-zheng
 * @Date 2022/8/15 17:08
 * @Version 1.0
 **/
@MessageType(proto = Command.AllBlackList, classtype = FriendMessage.AllBlackListRequest.class)
public class CMD_AllBlackList extends Command<FriendMessage.AllBlackListRequest> {
    @Override
    public void execute(MessageSource messageSource, FriendMessage.AllBlackListRequest request) throws Exception {
        //玩家code
        String playerCode = messageSource.getTag();
        //举报玩家code
        String blackCode = request.getPlayerCode();
        FriendMessage.AllBlackListResponse.Builder response = FriendMessage.AllBlackListResponse.newBuilder();
        AllChatBlackModule allChatBlackModule = new AllChatBlackModule();
        try {
            if(allChatBlackModule.tryLock()){
                boolean isSucess = AllChatBlackModule.addAllChatBlack(playerCode, blackCode);
                if(!isSucess){
                    send(messageSource, response, Result.ChatCantReportRepeatedly);
                }
                send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            allChatBlackModule.unlock();
        }
    }

    private void send(MessageSource messageSource, FriendMessage.AllBlackListResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
